var page = 1;
var mypage = 1;
var pages = 1;
var num=0;
var uid, ws, datalist=[];
var defaultIndex = 1;
var list;
document.addEventListener('plusready',function(){
	uid = plus.storage.getItem('uid');//用户id

	ws = plus.webview.currentWebview();
	PullToRefresh(ws);
	plus.nativeUI.closeWaiting();
	$('.fabiao').on('tap',function(){
		goNewPage('bbs-publish.html', {spage:ws.id})
	})
	//点击切换
	$('#sliderSegmentedControl a').on('tap',function(){ 
		mui("#pullrefresh").pullRefresh().endPullupToRefresh(false);
		if(defaultIndex == parseInt($(this).index()+1)){
			return;
		}
		defaultIndex = parseInt($(this).index()+1);
		switch(defaultIndex){
			case 1:
				pullfresh_fun();
			break;
			case 2:
				pullfresh_fun(1); 
			break;		
		}
	})
	
	pullfresh_fun();
},false);


function pullfresh_fun(str){
	var server = apiRoot+"?m=home&c=bbs&a=bbsList";
	pages = page;
	if(str == 1){	//个人
		server += "&uid="+uid;
		pages = mypage;
	}
	console.log(page+" "+mypage);
	server += "&page="+pages;
	console.log(server);
	$.ajax({
		type :"get", 
		url :server,
		dataType:'json',
		success :function(data){
			if(data){  
				datalist = $.unique($.merge(datalist,data.list));
				//我的发表  
				$.each(data.list, function(k,v) {
			 list = '<div class="margin-bottom-5">'+
						'<div class="mui-table-view table-view-cell bbs-ico margin-bottom-1" data-id="'+v.aid+'">'+
							'<div class="overflow-h">'+
								'<i><img src="'+getAvatar(v.avatar)+'"/></i>'+
								'<div>'+
									'<li>'+v.name+'</li>'+
									'<li class="mui-h6">'+v.addtime+'</li>'+
								'</div>'+
							'</div>'+
							'<div class="ellipsis-3">'+ v.content+'</div>'+
							'<div class="bbs-img">';
					if(v.pro != ''){
						$.each(v.pro, function(key,value) {
							list += '<div><img src="'+webRoot+v.pro[key]+'" alt="" /></div>';
						});
					}
					list += '</div>'+
						'</div>'+
						'<div class="bbs-img-pl background overflow-h table-view-cell-mini-15">'+
							'<span class="iconfont">&#xe61d;<span>'+v.pingnum+'</span></span>'+
							'<span class="iconfont">&#xe61c;<span>'+v.zannum+'</span></span>'+
							'<span class="mui-text-right iconfont">'+
								'<a href="#picture" style="color:#929292;">&#xe61e;<span class="fengxiang-text">'+v.zhuannum+'</span></a>'+
							'</span>'+
						'</div>'+
					'</div>'; 
					$("#item"+defaultIndex+"mobile .mui-scroll").append(list);
				});
				$(".bbs-ico").unbind('tap');
				$(".bbs-ico").on('tap',function(){
					var aid = $(this).attr('data-id');
        			var info = {};
        			for (var i = 0; i < datalist.length; i++) { 
						if(datalist[i].aid == aid){
							info = datalist[i];break;
						}
					} 
        			info.spage = ws.id;
        			goNewPage('bbs-text.html',info);
				})
				
				if(pages == 1){
					num = data['num'];
				} 
				pullup (num);
			}else{
				pullup (1);
			}
			
		},
		error :function(e){
//			pullup (1);
			errortoast();
			console.log(JSON.stringify(e));
		}
	});
} 
function pullup (num) {
	switch(defaultIndex){ 
		case 1:page +=1;break; 
		case 2:mypage +=1;break;
	}
//	console.log(num+" "+defaultIndex+" "+page+" "+mypage+" "+pages);
	if(num == 1 || num == 0){
		mui("#pullrefresh").pullRefresh().disablePullupToRefresh();
	}else if(pages == num){
		mui("#pullrefresh").pullRefresh().endPullupToRefresh(true);
		mui("#pullrefresh").pullRefresh().disablePullupToRefresh();
	}else{
		mui("#pullrefresh").pullRefresh().endPullupToRefresh(false);
	}
}